package com.xckj.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xckj.dao.InterfaceMethodMapper;
import com.xckj.dto.InterfaceDto;
import com.xckj.dto.InterfaceMethodDto;
import com.xckj.dto.InterfaceSelectDto;
import com.xckj.entity.InterfaceMethod;
import com.xckj.service.InterfaceMethodService;
import com.xckj.utils.ReturnT;
import com.xckj.utils.SnowFlake;

/**
 * @description 接口方法名称
 * @author yanchf
 * @date 2024-01-06
 */
@Service
public class InterfaceMethodServiceImpl implements InterfaceMethodService {

	@Resource
	private InterfaceMethodMapper interfaceMethodMapper;


	@Override
	public Object insert(InterfaceMethod interfaceMethod) {

		// valid
		if (interfaceMethod == null) {
			return ReturnT.error("必要参数缺失");
        }
		interfaceMethod.setId(SnowFlake.getID());
		interfaceMethodMapper.insert(interfaceMethod);
        return ReturnT.success();
	}


	@Override
	public Object delete(InterfaceMethod interfaceMethod) {
		int ret = interfaceMethodMapper.delete(interfaceMethod.getMethodName());
		return ret>0?ReturnT.success():ReturnT.error();
	}


	@Override
	public Object update(InterfaceMethod interfaceMethod) {
		int ret = interfaceMethodMapper.update(interfaceMethod);
		return ret>0?ReturnT.success():ReturnT.error();
	}


	@Override
	public InterfaceMethod load(int id) {
		return interfaceMethodMapper.load(id);
	}


	@Override
	public Map<String,Object> pageList(int offset, int pagesize) {
		List<InterfaceMethod> pageList = interfaceMethodMapper.pageList(offset, pagesize);
		int totalCount = interfaceMethodMapper.pageListCount(offset, pagesize);
		List<InterfaceMethodDto> results = new ArrayList<>();
		if(pageList != null) {
			int count = 1;
			for(InterfaceMethod method : pageList) {
				InterfaceMethodDto dto = new InterfaceMethodDto(count, method.getMethodName(), method.getMethodName());
				results.add(dto);
				count ++;
			}
		}
		// result
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("pageList", results);
		result.put("totalCount", totalCount);

		return ReturnT.success(result);
	}


	@Override
	public Map<String, Object> allMethod(int offset, int pagesize) {
		List<InterfaceMethod> pageList = interfaceMethodMapper.pageList(offset, pagesize);
		int totalCount = interfaceMethodMapper.pageListCount(offset, pagesize);
		List<InterfaceDto> results = new ArrayList<>();
		if(pageList != null) {
			int count = 1;
			for(InterfaceMethod method : pageList) {
				InterfaceDto dto = new InterfaceDto(count + "", method.getMethodName());
				results.add(dto);
				count ++;
			}
		}
		// result
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("pageList", results);
		result.put("totalCount", totalCount);

		return ReturnT.success(result);
	}


	@Override
	public Map<String, Object> allMethodSelect(int offset, int pagesize) {
		List<InterfaceMethod> pageList = interfaceMethodMapper.pageList(offset, pagesize);
		int totalCount = interfaceMethodMapper.pageListCount(offset, pagesize);
		List<InterfaceSelectDto> results = new ArrayList<>();
		if(pageList != null) {
			for(InterfaceMethod method : pageList) {
				InterfaceSelectDto dto = new InterfaceSelectDto(method.getMethodName(), method.getMethodName());
				results.add(dto);
			}
		}
		// result
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("pageList", results);
		result.put("totalCount", totalCount);

		return ReturnT.success(result);
	}

}